package servlets;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import actions.UserActionLogin;
import actions.UserActionRegister;

/**
 * Servlet implementation class UserServlet
 */

//这是人事管理系统最主要的程序，做集中控制，然后跳转

@WebServlet("*.do")
public class UserServletByReflect extends HttpServlet {
		private static final long serialVersionUID = 1L;
	
		public UserServletByReflect() {
	        super();
	        // TODO Auto-generated constructor stub
	    }

		/**
		 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
		 */
		protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			// TODO Auto-generated method stub
			//response.getWriter().append("Served at: ").append(request.getContextPath());
			doPost(request, response);
		}

		/**
		 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
		 */
		public void doPost(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			//1得到传过来的名
			String name=request.getServletPath();//得到传来的访问路径：/login.do
			name=name.substring(1);
			name=name.substring(0,name.length()-3);//最新得到的是login
			
			try {
				//2反射通过名得到方法
				Method method=getClass().getDeclaredMethod(name, HttpServletRequest.class,HttpServletResponse.class);
				//3调用方法
				method.invoke(this, request,response);
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		public void login(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			System.out.println("login");
			new UserActionLogin().execute(request, response);
		}
		
		public void register(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			System.out.println("register");
			new UserActionRegister().execute(request, response);
		}
	}
